package com.qf.game.servlet;

import com.qf.framwork.annotation.ResponseBody;
import com.qf.framwork.base.BaseServlet;
import com.qf.framwork.page.PageHelper;
import com.qf.game.entity.Orders;
import com.qf.game.entity.Player;
import com.qf.game.service.OrdersService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

/**
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@WebServlet("/orderServlet")
public class OrderServlet extends BaseServlet {

    private OrdersService ordersService = new OrdersService();

    /**
     * 添加订单
     */
    @ResponseBody
    public String insertOrders(Double money, HttpSession session){
        Player player = (Player) session.getAttribute("login_user");
        //生成订单对象
        Orders orders = new Orders()
                .setOrderId(UUID.randomUUID().toString())
                .setMoney(money)
                .setPid(player.getId())
                .setHappeyBean(BigDecimal.valueOf(money).multiply(BigDecimal.valueOf(100)).setScale(0).intValue());

        //调用service保存订单
        int oid = ordersService.insertOrders(orders);

        return "{\"oid\":" + oid + "}";
    }

    /**
     * 订单列表查询
     * @return
     */
    @PageHelper
    public String orderList(HttpSession session, HttpServletRequest request){
        //用户信息
        Player player = (Player) session.getAttribute("login_user");
        List<Orders> orders = ordersService.queryOrderList(player.getId());
        request.setAttribute("orders", orders);
        return "orderlist.jsp";
    }
}
